﻿using System;
using NUnit.Framework;
using System.Collections;
using System.Collections.Generic;
using CommonFramework.Serialize.BytesArray;
using Net;

namespace NetServerTest
{
    [TestFixture]
    public class TestNetPacket
    {
        private NetPakcetMove _pakcet;

        [SetUp]
        public void BeforePerTest()
        {
            _pakcet = new NetPakcetMove()
            {
                desc = "10.1.20.85:2333",
                x = 3.24f,
                y = 923671.1234f,
                z = -23241.231f,
            };
        }

        [Test]
        public void TestPackUnpack_OnSameType_MatchSameData()
        {
            BytesBuffer buffer = new BytesBuffer();
            _pakcet.WriteToBytes(buffer.GetWriter());

            NetPakcetMove pakcet = new NetPakcetMove();
            pakcet.ReadFromBytes(buffer.GetReader());

            Assert.That(_pakcet.desc,Is.EqualTo(pakcet.desc));
            Assert.That(_pakcet.x, Is.EqualTo(pakcet.x));
            Assert.That(_pakcet.y, Is.EqualTo(pakcet.y));
            Assert.That(_pakcet.z, Is.EqualTo(pakcet.z));
        }

        [Test]
        public void TestPacketName_OnGet_IsTypeFullName()
        {
            Assert.That(_pakcet.PacketName,Is.EqualTo( _pakcet.GetType().FullName));
        }

        [Test]
        public void TestPacketName_OnEncodeAndDecode_AreEqual()
        {
            BytesBuffer buffer = new BytesBuffer();
            NetUtil.EncodePacketName(_pakcet, buffer.GetWriter());

            string name= NetUtil.DecodePacketName(buffer.GetReader());

            Assert.That(name,Is.EqualTo(_pakcet.PacketName));
        }

        [Test]
        [TestCase(0)]
        [TestCase(1)]
        [TestCase(2)]
        [TestCase(5)]
        public void TestPacketName_OnDecodeNotEnough_ReturnEmptyName(int bytesLen)
        {
            BytesBuffer buffer = new BytesBuffer();
            NetUtil.EncodePacketName(_pakcet,buffer.GetWriter());

            BytesBuffer tempBytes = new BytesBuffer(bytesLen);
            tempBytes.GetWriter().WriteBytes(buffer.Bytes, buffer.DataStart(), bytesLen);

            string name = NetUtil.DecodePacketName(tempBytes.GetReader());
            Assert.That(name, Is.Empty);
        }
    }
}
